﻿using System;
using System.Collections.Generic;
using System.Text;
using Pixysoft.Tools;
using Pixysoft.Weblications.Sango.Formular;
using Pixysoft.Framework.Verifications;

namespace Pixysoft.Weblications.Sango.Dao
{
    class UsrActivityDao
    {
        public static Entities.Usr_Activity createHistory(Entities.Usr_Profile profile)
        {
            long currentdate = GlobalTimer.Instance.ToTimeSequence();

            Entities.Usr_Activity orm = Entities.Local.Usr_Activity;
            orm.Usercode = profile.Usercode;
            orm.Nickname = profile.Nickname;
            orm.Refreshallowancedate = currentdate;
            orm.Allowancecount = 0;
            orm.Refreshbuildingdate = currentdate;
            orm.Buildingcount = 0;
            orm.Refreshscoutdate = currentdate;
            orm.Scoutcount = 0;
            orm.Forcescoutcount = 0;
            orm.Refreshbattledate = currentdate;
            orm.Usedbattlecount = 0;
            orm.Freebattlecount = BattleFormular.factor_max_battle;
            orm.Freebattletypecode = StageFormular.createFreeBattleTypeCode();
            orm.Refreshstrategydate = currentdate;
            orm.Strategycount = 0;
            orm.Refreshfreerecuitdate = currentdate;
            orm.Freerecuitcount = 0;
            orm.Battlebuycount = 0;
            orm.Tradecount = 0;
            orm.Refreshtradedate = currentdate;
            return orm;
        }

        public static Entities.Usr_Activity getHistory(string usercode)
        {
            Entities.Usr_Activity orm = SettingHelper.GetNoebe().EntityNoebeCommand.SelectByPk<Entities.Usr_Activity>(usercode);

            if (orm == null)
                throw Exceptions.VerificationFailedException(usercode);

            refreshHistory(orm);

            return orm;
        }

        protected static bool refreshHistory(Entities.Usr_Activity orm)
        {
            bool updated = false;

            // 检查巡视

            if (PlayerFormular.isNextScoutDay(orm.Refreshscoutdate))
            {
                LoggerHelper.Debug("ACTIVITY HISTORY refresh scout, Usercode:{0} Nickname:{1} Refreshscoutdate:{2} Scoutcount:{3} Forcescoutcount:{4}", //
                       orm.Usercode, orm.Nickname, orm.Refreshscoutdate, orm.Scoutcount, orm.Forcescoutcount);
                orm.Scoutcount = 0;
                orm.Forcescoutcount = 0;
                updated = true;
            }
            orm.Refreshscoutdate = GlobalTimer.Instance.ToTimeSequence();

            // 检查俸禄

            if (PlayerFormular.isNextAllowanceDay(orm.Refreshallowancedate))
            {
                LoggerHelper.Debug("ACTIVITY HISTORY refresh allowance, Usercode:{0} Nickname:{1} Refreshallowancedate:{2} Allowancecount:{3}", //
                        orm.Usercode, orm.Nickname, orm.Refreshallowancedate, orm.Allowancecount);
                orm.Allowancecount = 0;
                updated = true;
            }
            orm.Refreshallowancedate = GlobalTimer.Instance.ToTimeSequence();

            // 检查建筑升级

            if (BuildingFormular.isNextBuildingDay(orm.Refreshbuildingdate))
            {
                LoggerHelper.Debug("ACTIVITY HISTORY refresh building-upgrade, Usercode:{0} Nickname:{1} Refreshbuildingdate:{2} Buildingcount:{3}", //
                        orm.Usercode, orm.Nickname, orm.Refreshbuildingdate, orm.Buildingcount);
                orm.Buildingcount = 0;
                updated = true;
            }
            orm.Refreshbuildingdate = GlobalTimer.Instance.ToTimeSequence();

            // 检查科技升级

            if (StrategyFormular.isNextStrategyDay(orm.Refreshstrategydate))
            {
                LoggerHelper.Debug("ACTIVITY HISTORY refresh strategy, Usercode:{0} Nickname:{1} Refreshstrategydate:{2} Strategycount:{3}", //
                        orm.Usercode, orm.Nickname, orm.Refreshstrategydate, orm.Strategycount);
                orm.Strategycount = 0;
                updated = true;
            }
            orm.Refreshstrategydate = GlobalTimer.Instance.ToTimeSequence();

            // 每小时增加军令

            if (BattleFormular.isIncreaseBattleTime(orm.Refreshbattledate))
            {
                // 可能跨了很多个小时

                LoggerHelper.Debug("ACTIVITY HISTORY increase battle times, Usercode:{0} Nickname:{1} Refreshbattledate:{2} Freebattlecount:{3}", //
                        orm.Usercode, orm.Nickname, orm.Refreshbattledate, orm.Freebattlecount);
                orm.Freebattlecount = Math.Min(orm.Freebattlecount + BattleFormular.IncreaseBattleCount(orm.Refreshbattledate), BattleFormular.factor_max_battle);
                updated = true;
            }

            // 刷新系统免费攻击军令

            if (BattleFormular.isFreeBattleRefreshtime(orm.Refreshbattledate))
            {
                LoggerHelper.Debug("ACTIVITY HISTORY free battle refresh time, Usercode:{0} Nickname:{1} Refreshbattledate:{2} Freebattletypecode:{3}", //
                        orm.Usercode, orm.Nickname, orm.Refreshbattledate, orm.Freebattletypecode);
                orm.Freebattletypecode = StageFormular.createFreeBattleTypeCode();
                updated = true;
            }

            // 刷新当日已经攻击次数

            if (BattleFormular.isNextBattleDay(orm.Refreshbattledate))
            {
                LoggerHelper.Debug("ACTIVITY HISTORY refresh total battle count, Usercode:{0} Nickname:{1} Refreshbattledate:{2} Usedbattlecount:{3}", //
                        orm.Usercode, orm.Nickname, orm.Refreshbattledate, orm.Usedbattlecount);
                orm.Usedbattlecount = 0;
                updated = true;
            }
            orm.Refreshbattledate = GlobalTimer.Instance.ToTimeSequence();


            // 收获资源刷新

            if (ResourceFormular.IsHarvestRefresh(orm.Resourceharvestdate))
            {
                LoggerHelper.Debug("ACTIVITY HISTORY refresh resource harvest count, Usercode:{0} Nickname:{1} refreshdate:{2} refreshvalue:{3}", //
                    orm.Usercode, orm.Nickname, orm.Resourceharvestdate, orm.Resourceharvestcount);
                orm.Resourceharvestcount = 0;
                updated = true;
            }
            orm.Refreshbattledate = GlobalTimer.Instance.ToTimeSequence();

            if (updated)
            {
                SettingHelper.GetNoebe().EntityNoebeCommand.Update(orm);
            }

            return updated;
        }
    }
}
